﻿using System;
using System.Text;
using System.IO;
using System.Reflection;

namespace DataCoreTechnologies.SqlServer
{
    public sealed class Diagnostic
    {
        #region Public
        //Methods
        public static void WriteLine(string line)
        {
            if (_diagnostic == null) _diagnostic = new Diagnostic();
            _streamWriter.WriteLine(DateTime.Now.ToString() + ":" + line);
            _streamWriter.Flush();
        }
        public static void Write(string line)
        {
            if (_diagnostic == null) _diagnostic = new Diagnostic();
            _streamWriter.Write(DateTime.Now.ToString() + ":" + line);
            _streamWriter.Flush();
        }
        public static void Close() { if (_diagnostic != null) _streamWriter.Close(); _diagnostic = null; }
        #endregion
        #region Private
        //Constructors
        private Diagnostic()
        {
            string filePath = Assembly.GetEntryAssembly().Location;
            int lastSlash = filePath.LastIndexOf(@"\");
            filePath = filePath.Substring(0, lastSlash + 1);
            filePath += "diagnostics.txt";

            if (File.Exists(filePath)) File.Delete(filePath);
            _streamWriter = new StreamWriter(filePath);

        }
        //Properties
        private static Diagnostic _diagnostic = null;
        private static StreamWriter _streamWriter = null;
        #endregion
    }
}
